﻿<?php


$id=$_GET['id'];
if(!empty($_POST)){
    $obj=new mysql($config);
    if(!empty($_SERVER['HTTP_X_REQUESTED_WITH'])&&$_POST['flag']==-1){
        $val=$_POST['val'];
        $filed=$_POST['filed'];
        $data=array(
           $filed=>$val,
            );
        echo $obj->update($data,array('id'=>$id),'news');
        die;
    }

    if(!empty($_SERVER['HTTP_X_REQUESTED_WITH'])&&$_POST['flag']==0){
        $statue=$_POST['statue'];
        echo $obj->update(array('statue'=>$statue),array('id'=>$id),'news');
        die;
    }





    $myfile=$_FILES['myfile'];
    
    //判断当前是否上传图片，有上传则把原先的图片删除
    if($myfile['error']==0){
        $sql="select img from `news` where `id`={$id}";
        $date=$obj->getOne($sql);
        if(file_exists($date['img'])){
            unlink($date['img']);               
        }

        if(!is_uploaded_file($myfile['tmp_name'])){
               die('非法上传');
        }
        $allow=array('png','jpg','jpeg','gif');
        $ori=explode('.',$myfile['name']);
        $origanl=end($ori);
        if(!in_array($origanl,$allow)){
                die('文件类型有误');
        }

        if(!is_dir("upload/".date('Y-m'))){
                mkdir("upload/".date('Y-m'),0771,true);
        }
        
        $filename=substr(md5(uniqid()),0,18);
        $path="upload/".date('Y-m');
        $rootpath=$path.'/'.$filename.'.'.$origanl;
        if(!move_uploaded_file($myfile['tmp_name'],$rootpath)){
                die('上传文件有误');
        }

        $_POST['img']=$rootpath;
    }

  
    
    $num=$obj->update($_POST,array('id'=>$id),'news');

    if($num!==false){
        echo "<script>alert('修改成功');window.reload();</script>";
    }else{
        echo "<script>alert('修改失败');window.reload();</script>";
    }

}

$obj=new mysql($config);
$sql="select * from `news` where `id`={$_GET['id']}";
$date=$obj->getOne($sql);

require_once (VIEW_PATH.'admin/layout.html');

